package services;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class LoadVAMatrix implements Runnable{
	private String workbookname;
	
	public static String[] sImageType = {"100GB drive D: image", "IVT", "New VNC server", "64bit OS"};
	//[class name, image type, deployment folder]
	public static Vector<Vector> vaMatrix;
	
	public LoadVAMatrix(){
		workbookname = "";
		vaMatrix = new Vector<Vector>();
		
	}
	
	public void setWorkbookName(String fileName) {
		workbookname = fileName;
	}
	
	private String parseCourseName(String inputName){
		
		String outputName = "";
		Pattern pn = null;
		Matcher mr = null;
		boolean isAbbr = false;
	
		String sCompanyName = "";
		String sProductName = "";
		String sVersion = "";
		String sOther = "";

		pn = Pattern.compile("^[A-Z]+$");
		mr = pn.matcher(sProductName);
		if (mr.matches()){
			isAbbr = true;
		}
		System.out.println("input" + inputName + "c: " + sCompanyName + " p:" + sProductName + " v:" + sVersion
				+ " O:" + sOther);
		return outputName;
	}

	private void loadVAMatrix(){
		XSSFWorkbook wb;
		XSSFSheet sheet;
		XSSFRow row;
		XSSFCell cell;
		FileInputStream fis = null;
		Pattern p = null;
		Matcher m = null;
		
		String vaClassName = "";
		String vaImageType = "";
		String vaDPFolder = "";
		
		try {
			fis = new FileInputStream(workbookname);
			wb = new XSSFWorkbook(fis);
			//	Class name 2,	100GB drive D: image 3,	IVT	New VNC server 4, 64bit OS4 5, 	
			//	Possible pods 6,	Deployment tool 7,	Deployment Folder 8,
			//	Who Deploy	9, Comments	13 10
			sheet = wb.getSheet("Physical");
			for (int r=11; r<sheet.getPhysicalNumberOfRows(); r++){
				row = sheet.getRow(r);
				XSSFCell tmpCell = row.getCell(1);
				//
				if (tmpCell == null){break;}
				for (int c=2; c<12; c++){
					if (tmpCell.getCellType() == XSSFCell.CELL_TYPE_STRING){
						cell = row.getCell(c);
						if (cell != null){
							switch (c){
							case 2: // class name
								if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING){
									vaClassName = parseCourseName(cell.getStringCellValue());
								}
								
								break;
							default:
								break;
							}
						}
					}
				}
			}			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	public void run() {
		if (!workbookname.isEmpty()){
			//loadVAMatrix();
			//DicT dtemp = new DicT();
			//dtemp.loadDicT();
		}
	}
	
}
